--[[--------------------------------------------------------------------------------

	Bellah of Stormrage - Jan 2009
	Lootlist: Simple loot tracking addon. 
	tracks need / greed / dkp in parties/raids

----------------------------------------------------------------------------------]]

local _LL_ADDON_NAME = "Lootlist"
local _LL_ADDON_VERSION = "1.0"

Lootlist = LibStub("AceAddon-3.0"):NewAddon(_LL_ADDON_NAME, "AceConsole-3.0", "AceEvent-3.0")

-- get the Locale defs
local L = LibStub("AceLocale-3.0"):GetLocale(_LL_ADDON_NAME, true)
-- get the frames library
local frameLib = LibStub("LootListFrames")
local deformat = LibStub("Deformat-2.0")

local config = {}

local itemLevels = {"Poor", "Common", "Uncommon", "Rare", "Epic", "Legendary", "Artifact"}

local configDefaults = {
	minItemLvl = 4, -- Epic
	ignoreItems = {},
}

local options = {
    name = "Lootlist",
    handler = Lootlist,
    type = "group",
    args = {
		start = {
			guiHidden = true,
			type = "input",
			name = "start",
			desc = "Start tracking",
			get = false,
			set = "Start",
		},
		stop = {
			guiHidden = true,
			type = "execute",
			name = "stop",
			desc = "Stop tracking",
			func = "Stop",
		},
		config = {
			guiHidden = true,
			type = "execute",
			name = "config",
			desc = "Configure Lootlist",
			func = function ()
					InterfaceOptionsFrame_OpenToCategory(_ADDONNAME)
				end,
		},
		show = {
			guiHidden = true,
			type = "execute",
			name = "show",
			desc = "Show Lootlist",
			func = function ()
					Lootlist:Show()
				end,
		},
		settingsHeader = {
			cmdHidden = true,
			type = "header",
			name = "Lootlist settings",
			order = 1,
		},
		itemLvl = {
			cmdHidden = true,
			type = "select",
			name = "Min. item level",
			desc = "Sets minimum item level to track (default: Epic)",
			order = 2,
			values = itemLevels,
			get = function()
					return config.minItemLvl + 1 -- tables start at index 1, GetItemInfo() returns item lvl at index 0
				end,
			set = function (info, value)
					config.minItemLvl = value - 1
				end,
		},
		miscHeader = {
			cmdHidden = true,
			type = "header",
			name = "Miscellaneous settings",
			order = 98,
		},
		debug = {
			cmdHidden = true,
			type = "toggle",
			name = "debug",
			desc = "turn debug info on|off",
			order = 99,
			get = function ()
					return config.debug
				end,
			set = function(info, value)
					config.debug = value
					Lootlist:SetDebug()
				end,
		},
    },
}

LibStub("AceConfig-3.0"):RegisterOptionsTable(_LL_ADDON_NAME, options, {_LL_ADDON_NAME, "ll"})

function Lootlist:OnInitialize()
	local defaults = {
		profile = configDefaults
	}
	
	-- get stored config (or store defaults)
	self.db = LibStub("AceDB-3.0"):New(_LL_ADDON_NAME .. "DB", defaults, "global")
	config = self.db.profile

	self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions(_LL_ADDON_NAME, _LL_ADDON_NAME)
	
	self:Print(string.format("|cffffff00%s v%s loaded (locale: %s). Use \"/ll\" for info", _LL_ADDON_NAME, _LL_ADDON_VERSION, L["locale"]))
end

function Lootlist:OnEnable()
    -- Called when the addon is enabled
--    self:dbgPrint(_ADDONNAME .. " - onEnable()")
end

function Lootlist:OnDisable()
    -- Called when the addon is disabled
    self:Stop()
--    self:dbgPrint(_ADDONNAME .. " - onDisable()")
end

function Lootlist:OnUpdate()

end

function Lootlist:Start()
	self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED", "ProcessKill")
	self:RegisterEvent("CHAT_MSG_LOOT", "ProcessLoot")
	self:Print("Started loot and kill monitoring")
	self:Print ("LOOT_ITEM: " .. LOOT_ITEM)
	self:Print ("LOOT_ITEM_SELF: " .. LOOT_ITEM_SELF)
	self:Print ("LOOT_ITEM_MULTIPLE: " .. LOOT_ITEM_MULTIPLE)
end

function Lootlist:Stop()
	self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
	self:UnregisterEvent("CHAT_MSG_LOOT")
	self:Print("Stopped loot and kill monitoring")
end

function Lootlist:ProcessKill()
	if arg2 ~= "UNIT_DIED" then return end
	
	self:Print (string.format("%s died", arg7))
end

function Lootlist:ProcessLoot(event)
	local msg = arg1

--	pattern = string.gsub(string.gsub(value, "(%%s)", "(.+)"), "(%%d)", "(.+)");
--	local start, fin, arg1, arg2, arg3 = string.find(msg, pattern)

	local player, item, amt = deformat(msg, LOOT_ITEM_MULTIPLE)
	amt = amt or "01"
	
	if not player then
		item = deformat(msg, LOOT_ITEM_SELF)
		if item then
			player = UnitName("player")
		end
	end
	local name, link, quality = GetItemInfo(item)
	
	-- seriously bad hack - must have/force items in/into local cache to use GetItemInfo
	quality = quality or 0
	if type(item) == "string" and type(player) == "string" then
		self:Print (string.format("Looter %s received %d %s (%s)", player, amt, name, itemLevels[quality+1]))
	end
end

function Lootlist:SetDebug()
	if config.debug then
	    self:Print("|cffffff00Debug info enabled")
	else
	    self:Print("|cffffff00Debug info disabled")
	end
	framelib:SetDebug(config.debug)
end

function Lootlist:dbgPrint(msg)
	if config.debug then 
		self:Print("|cffffff00Debug - " .. msg)
	end
end
